package pers.rcl.common.util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * java8新日期API实现
 *
 * @author liuqiang
 */
public class LocalDateUtils {

    public static final String DATE_PATTON_1 = "yyyy-MM-dd";
    public static final String DATE_PATTON_2 = "yyyy/MM/dd";
    public static final String DATE_PATTON_3 = "yyyyMMdd";
    public static final String DATE_PATTON_4 = "yyyyMM";
    public static final String DATE_TIME_PATTON_1 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_PATTON_2 = "yyyy/MM/dd HH:mm:ss";
    public static final String DATE_TIME_PATTON_3 = "yyyyMMddHHmmss";
    public static final String DATE_TIME_PATTON_4 = "yyyy-MM-dd HH:mm:ss:SSS";

    public static final String DATE_TIME_PATTON_5 = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    private static final ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");

    private static final DateTimeFormatter DATE_TIME_PATTON_1_Formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTON_1);
    private static final DateTimeFormatter DATE_TIME_PATTON_3_Formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTON_3);
    private static final DateTimeFormatter DATE_PATTON_3_Formatter = DateTimeFormatter.ofPattern(DATE_PATTON_3);
    private static final DateTimeFormatter DATE_PATTON_4_Formatter = DateTimeFormatter.ofPattern(DATE_PATTON_4);
    private static final DateTimeFormatter DATE_TIME_PATTON_4_Formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTON_5);

    /**
     * 获取当前系统时间的yyyyMMddHHmmss字符串
     *
     * @return String
     */
    public static String getDateTimeStrForNow() {
        return LocalDateTime.now(ZONE_ID).format(DATE_TIME_PATTON_3_Formatter);
    }

    /**
     * 获取当前系统时间的yyyyMMddHHmmss字符串
     *
     * @return String
     */
    public static String getDateTimeStrForNow(String formatter) {
        return LocalDateTime.now(ZONE_ID).format(DateTimeFormatter.ofPattern(formatter));
    }

    /**
     * 获取当前系统时间的yyyy-MM-dd HH:mm:ss字符串
     *
     * @return String
     */
    public static String getDateTimeStrWithSepForNow() {
        return LocalDateTime.now(ZONE_ID).format(DATE_TIME_PATTON_1_Formatter);
    }

    /**
     * 获取当前系统时间的yyyy-MM-dd'T'HH:mm:ss'Z'字符串 ----神州云合使用
     *
     * @return String
     */
    public static String getDateTimeStrWithSepForYunHe() {
        return LocalDateTime.now(ZONE_ID).format(DATE_TIME_PATTON_4_Formatter);
    }

    /**
     * 获取当前系统时间的yyyyMMdd字符串
     *
     * @return String
     */
    public static String getDateStrForNow() {
        return LocalDate.now(ZONE_ID).format(DATE_PATTON_3_Formatter);
    }

    /**
     * 获取当前系统时间上一个月的yyyyMM字符串
     *
     * @return String
     */
    public static String getDateStrForNow4() {
        LocalDate localDate = LocalDate.now();
        int year = localDate.getYear();
        int month = localDate.getMonthValue();
        if (month == 1) {
            year -= 1;
            month = 12;
        } else {
            month -= 1;
        }
        return LocalDate.of(year, month, localDate.getDayOfMonth()).format(DATE_PATTON_4_Formatter);
    }

    public static void main(String[] args) {
        System.out.println(getDateStrForNow4());
    }

    /**
     * 获取前一天的yyyyMMdd字符串
     *
     * @return String
     */
    public static String getPreviousDateStr() {
        return LocalDate.now(ZONE_ID).plus(-1L, ChronoUnit.DAYS).format(DATE_PATTON_3_Formatter);
    }

    /**
     * 获取当前Unix时间戳字符串(秒)
     *
     * @return String
     */
    public static String getNowTimeStampStr() {
        return String.valueOf(Instant.now().getEpochSecond());
    }

    /**
     * 获取当前Unix时间戳字符串(毫秒)
     *
     * @return String
     */
    public static String getNowTimeStampMillisStr() {
        return String.valueOf(Instant.now().toEpochMilli());
    }

    /**
     * 获取指定日期Unix时间戳(毫秒)
     *
     * @return String
     */
    public static Long getTimeStampFromStr(String date) {
        LocalDateTime parse = LocalDateTime.parse(date, DATE_TIME_PATTON_3_Formatter);
        return LocalDateTime.from(parse).atZone(ZONE_ID).toInstant().toEpochMilli();
    }

    /**
     * 获取指定日期Unix时间戳(毫秒)
     *
     * @return long
     */
    public static long getTimeStamp(LocalDateTime time) {
        return time.atZone(ZONE_ID).toInstant().toEpochMilli();
    }

    /**
     * 时间戳(毫秒)转换为时间
     *
     * @return String
     */
    public static LocalDateTime timeStampToDatetime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZONE_ID);
    }

    /**
     * 将Date转换为指定格式的字符串
     *
     * @return
     */
    public static String dataToStrForrmate(Date date, String formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

}
